﻿using log4net;
using MaterialTypeRecognition.Shell.Logs;
using MaterialTypeRecognition.Shell.SaveInfos;
using MaterialTypeRecognition.Shell.SaveInfoService;
using MaterialTypeRecognition.Shell.Utils.Devices;
using System;
using System.Threading.Tasks;

namespace MaterialTypeRecognition.Shell.DeviceServices
{
    public class DeviceWriteEventArgs : EventArgs
    {
        public string Message { get; set; }

        public bool IsSuccess { get; set; }

        public DeviceWriteEventArgs(string message, bool isSuccess)
        {
            Message = message;
            IsSuccess = isSuccess;
        }
    }

    public class SiemensSmart200Service
    {
        public event EventHandler<DeviceWriteEventArgs>? DeviceWrite;

        private SiemensSmart200 _smart200;

        private readonly ISaveInfoService _saveInfoService;

        private readonly ILog _appLog;

        public SiemensSmart200Service(SiemensSmart200 siemensSmart200, ISaveInfoService saveInfoService, ILog4NetLoggerFactory log4NetLoggerFactory)
        {
            _smart200 = siemensSmart200;
            _saveInfoService = saveInfoService;
            _appLog = log4NetLoggerFactory.GetAppLogger();
        }

        /// <summary>
        /// 测试一下设备可用性
        /// </summary>
        public async Task<bool> TestConnectAsync(string ipAddress, int port, int milliseconds = 200)
        {
            try
            {
                // 因为读写不频繁，短链接即可
                await _smart200.ConnectAsync(ipAddress, port);
                await Task.Delay(milliseconds);
                await _smart200.CloseAsync();

                return true;
            }
            catch (Exception ex) 
            {
                _appLog.Error(ex);
                return false;
            }
        }

        /// <summary>
        /// 写入白色信号
        /// </summary>
        public async Task WriteWhiteSignalAsync(string ipAddress, int port)
        {
            try
            {
                // 因为读写不频繁，短链接即可
                await _smart200.ConnectAsync(ipAddress, port).ConfigureAwait(false);

                var saveIfno = await _saveInfoService.ReadAsync<AppSettingSaveInfo>();
                await _smart200.WriteBoolAsync(saveIfno.WhiteSignalAddress, true);

                await _smart200.CloseAsync();
                DeviceWrite?.Invoke(this, new DeviceWriteEventArgs("写入白色类别极片信息！", true));
            }
            catch (InvalidOperationException ex)
            {
                DeviceWrite?.Invoke(this, new DeviceWriteEventArgs(ex.Message, false));
            }
        }

        /// <summary>
        /// 写入红色信号
        /// </summary>
        public async Task WriteRedSignalAsync(string ipAddress, int port)
        {
            try
            {
                // 因为读写不频繁，短链接即可
                await _smart200.ConnectAsync(ipAddress, port).ConfigureAwait(false);

                var saveIfno = await _saveInfoService.ReadAsync<AppSettingSaveInfo>();
                await _smart200.WriteBoolAsync(saveIfno.RedSignalAddress, true);

                await _smart200.CloseAsync();
                DeviceWrite?.Invoke(this, new DeviceWriteEventArgs("写入红色类别极片信息！", true));
            }
            catch (InvalidOperationException ex)
            {
                DeviceWrite?.Invoke(this, new DeviceWriteEventArgs(ex.Message, false));
            }
        }

        /// <summary>
        /// 写入黄色信号
        /// </summary>
        public async Task WriteYellowSignalAsync(string ipAddress, int port)
        {
            try
            {
                // 因为读写不频繁，短链接即可
                await _smart200.ConnectAsync(ipAddress, port).ConfigureAwait(false);

                var saveIfno = await _saveInfoService.ReadAsync<AppSettingSaveInfo>();
                await _smart200.WriteBoolAsync(saveIfno.YellowSignalAddress, true);

                await _smart200.CloseAsync();
                DeviceWrite?.Invoke(this, new DeviceWriteEventArgs("写入黄色类别极片信息！", true));
            }
            catch (InvalidOperationException ex)
            {
                DeviceWrite?.Invoke(this, new DeviceWriteEventArgs(ex.Message, false));
            }
        }
    }
}
